package com.sgb.kjwl.view.ui.activity.self.websiteManage;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.graphics.drawable.Drawable;

import androidx.recyclerview.widget.GridLayoutManager;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityWebsitePhotoEdit3Binding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.WebsitePhotoEntity;
import com.sgb.kjwl.view.ui.adapter.self.website.WebsitePhotoEditAdapter3;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.self.website.WebsitePhotoEditModel3;
import com.swgk.core.base.di.AppComponent;

import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;

public class WebsitePhotoEditActivity3 extends AppActivity {

    private ActivityWebsitePhotoEdit3Binding binding;
    private SpannableStringBuilder spand;
    private List<WebsitePhotoEntity> photoList;
    private WebsitePhotoEditAdapter3 editAdapter;
    private boolean isAll = false;
    private String companyId = "";

    @Inject
    WebsitePhotoEditModel3 editModel;

    public static void start(Context context, List<WebsitePhotoEntity> list, String id){
        Intent intent = new Intent(context,WebsitePhotoEditActivity3.class);
        intent.putExtra("list",(Serializable) list);
        intent.putExtra("companyId",id);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_website_photo_edit3);
        binding.actionBarImageRight.setOnClickListener(view->finish());
        binding.selectAll.setOnClickListener(view -> {
            isAll = !isAll;
            setSelectNum(isAll?photoList.size():0);
            editAdapter.setSelectAll(isAll);
        });

        binding.deletePhoto.setOnClickListener(view -> {
            deleteDialog();
        });

        spand = new SpannableStringBuilder("");
    }

    @Override
    protected void init() {
        companyId = getIntent().getStringExtra("companyId");
        photoList = (List<WebsitePhotoEntity>)getIntent().getSerializableExtra("list");

        editAdapter = new WebsitePhotoEditAdapter3(this,photoList);
        binding.listView.setLayoutManager(new GridLayoutManager(this,3));
        binding.listView.setAdapter(editAdapter);

        editAdapter.setSelectListener(urls -> {
            setSelectNum(urls.size());
        });
    }

    private void setSelectNum(int size){
        String str = "(已选择"+ size+"张图片)";
        spand.clear();
        spand.append(str);
        spand.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.new_org)),4,str.indexOf("张"), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        binding.selectNum.setText(spand);
        Drawable allDrawable;
        if(size == photoList.size()){
            isAll = true;
            allDrawable = getResources().getDrawable(R.mipmap.common_checked);
        }else{
            isAll = false;
            allDrawable = getResources().getDrawable(R.mipmap.commom_uncheck);
        }
        allDrawable.setBounds(0,0,allDrawable.getMinimumWidth(),allDrawable.getMinimumHeight());
        binding.selectAll.setCompoundDrawables(allDrawable,null,null,null);

        if(size>0){
            binding.deletePhoto.setTextColor(getResources().getColor(R.color.new_org));
            binding.deletePhoto.setEnabled(true);
        }else{
            binding.deletePhoto.setEnabled(false);
            binding.deletePhoto.setTextColor(getResources().getColor(R.color.color_969696));
        }
    }

    //删除弹窗
    private void deleteDialog() {
        View view = View.inflate(this, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        tv_title.setVisibility(View.VISIBLE);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("温馨提示");
        tv_message.setText("您确定删除所选"+ editAdapter.getSelectedUrls().size() +"照片?");

        //确定
        tv_logout_confirm.setOnClickListener(v -> {
            //删除
            editModel.deletePhotoData(editAdapter.getSelectedUrls());
            roundCornerDialog.dismiss();
        });

        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }
}